import inspect
import types
import os
from . import core
from . import logging
from . import chains


def include(filename):
    """loads the given python file into the caller's namespace"""
    namespace = inspect.stack()[1][0].f_globals
    logging.loginfo("INCLUDE", "including %r", filename)
    execfile(filename, namespace)

def cond(condition, obj):
    """conditionally inserts a module into the inputs list"""
    if condition:
        return obj
    else:
        return None

def load(filename):
    """loads a python file, returns a module object; does not contaminate 
    the caller's namespace"""
    mod = types.ModuleType(os.path.basename(filename).replace(".", "_"))
    mod.__file__ = os.path.abspath(filename)
    mod.__dict__.update(common_namespace)
    logging.loginfo("LOAD", "loading %r", filename)
    execfile(filename, mod.__dict__)
    return mod
    

common_namespace = {
    "core" : core,
    "logging" : logging,
    "chains" : chains,
    "INCLUDE" : include,
    "LOAD" : load,
    "IF" : cond,
    "Custom" : core.CustomParams,
}





